package com.jt.tenant.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.MaintainAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.Get4SShopList;
import com.jt.tenant.popwindow.SortListPopup;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MaintainActivity extends JTActivity implements View.OnClickListener {

    private TextView mTitleText;

    private ListView mListView;

    private List<Get4SShopList.FSList> mList = new ArrayList<>();

    private List<Get4SShopList.FSList> mAllList = new ArrayList<>();

    private MaintainAdapter mMaintainAdapter;

    private Get4SShopList mGet4SShopList;

    private LinearLayout mSortLin,mAreaLin;

    private SortListPopup mPop;

    private List<Get4SShopList.AreaList> mAreaList = new ArrayList<>();

    private List<Get4SShopList.AreaList> mMapList = new ArrayList<>();

    private TextView mTextSort,mTextArea;

    private int flag = 0;

    private String areatypeid = "";//0智能

    private String maptypeid = "0";//0全部

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain);

        initView();
    }

    private void initView(){

        mTitleText = (TextView) findViewById(R.id.tv_title_name);

        mTitleText.setText("保养");

        mSortLin = (LinearLayout) findViewById(R.id.lin_sort);

        mAreaLin = (LinearLayout) findViewById(R.id.lin_area);

        mTextSort = (TextView) findViewById(R.id.text_sort);

        mTextArea = (TextView) findViewById(R.id.text_area);

        mListView = (ListView) findViewById(R.id.list_maintain);

        mMaintainAdapter = new MaintainAdapter(this, mList);

        mListView.setAdapter(mMaintainAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MaintainActivity.this,MaintainShopActivity.class);
                Bundle bundle = new Bundle();
//                bundle.putString("name",mList.get(position).NAME);
//                bundle.putString("address",mList.get(position).ADDRESS);
//                bundle.putString("phone",mList.get(position).PHONE);
//                bundle.putString("img",mList.get(position).LOGO);
                bundle.putString("id",mList.get(position).ID);
//                bundle.putString("lat",mList.get(position).LAT);
//                bundle.putString("lon",mList.get(position).LON);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        mGet4SShopList = new Get4SShopList(new AsyCallBack<Get4SShopList.Info>() {

            @Override

            public void onSuccess(String toast, int type, Get4SShopList.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                mList.clear();

                mList.addAll(info.fSList);

                mAllList.addAll(info.fSList);

                getDistance();

                SortListByPro(mList);

                mMaintainAdapter.notifyDataSetChanged();

                mAreaList.clear();

                mAreaList.addAll(info.arList);

                mMapList.clear();

                mMapList.addAll(info.mapList);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(MaintainActivity.this, toast);
                finish();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!toast.equals("")) {

                    UtilToast.show(MaintainActivity.this, toast);

                }
            }
        });
        mGet4SShopList.execute(MaintainActivity.this);

        mSortLin.setOnClickListener(this);

        mAreaLin.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.lin_sort:

                if(mPop!=null){

                    if(mPop.isShowing())

                        mPop.dismiss();

                    else{

                        if(flag==0){
                            mPop.showAsDropDown(view);
                        }

                        else{
                            mPop.changeList(mMapList);

                            mPop.showAsDropDown(view);

                            flag=0;
                        }

                    }

                }else{

                    mPop = new SortListPopup(this,mMapList);

                    initPopListener();

                    mPop.showAsDropDown(view);

                    flag=0;

                }

                break;

            case R.id.lin_area:

                if(mPop!=null){

                    if(mPop.isShowing())

                        mPop.dismiss();

                    else{

                        if(flag==1){
                            mPop.showAsDropDown(view);
                        }

                        else{
                            mPop.changeList(mAreaList);

                            mPop.showAsDropDown(view);

                            flag=1;
                        }

                    }

                }else{

                    mPop = new SortListPopup(this,mAreaList);

                    initPopListener();

                    mPop.showAsDropDown(view);

                    flag=1;

                }

                break;


        }
    }

    private void initPopListener(){

        mPop.setOnItemClickListener(new SortListPopup.OnItemClick() {
            @Override
            public void onItemClick(Get4SShopList.AreaList area) {
                if(area.AREAID.equals("6003")){
                    SortListByPro(mAllList);
                    mTextSort.setText(area.AREANAME);
                }else if(area.AREAID.equals("6004")){
                    //离我最近
                    SortListByDis(mAllList);
                    mTextSort.setText(area.AREANAME);
                }else{
                    maptypeid = area.AREAID;
                    mTextArea.setText(area.AREANAME);
                }
                mList.clear();
                if(maptypeid.equals("0")){
                    mList.addAll(mAllList);
                }else{
                    for(int i=0;i<mAllList.size();i++){
                        if(mAllList.get(i).AREAID.equals(maptypeid)){
                            mList.add(mAllList.get(i));
                        }
                    }
                }
                changeList(area.AREAID);
                mMaintainAdapter.notifyDataSetChanged();
            }
        });

    }

    private void changeList(String id){

        if(id.equals("6003")||id.equals("6004")){

            for(int i=0;i<mMapList.size();i++){
                if(mMapList.get(i).AREAID.equals(id)){
                    mMapList.get(i).FLAG="1";
                }else{
                    mMapList.get(i).FLAG="0";
                }
            }

        }else{

            for(int i=0;i<mAreaList.size();i++){
                if(mAreaList.get(i).AREAID.equals(id)){
                    mAreaList.get(i).FLAG="1";
                }else{
                    mAreaList.get(i).FLAG="0";
                }
            }

        }
    }

    private List<Get4SShopList.FSList> SortListByPro(List<Get4SShopList.FSList> list){

        Collections.sort(list, new Comparator<Get4SShopList.FSList>(){

            /*
             * 返回负数表示：o1 小于o2，
             * 返回0 表示：o1和o2相等，
             * 返回正数表示：o1大于o2。
             */
            public int compare(Get4SShopList.FSList o1, Get4SShopList.FSList o2) {

                //按照学生的年龄进行升序排列
                if(Integer.parseInt(o1.PRIORITY) > Integer.parseInt(o2.PRIORITY)){
                    return -1;
                }
                if(Integer.parseInt(o1.PRIORITY) == Integer.parseInt(o2.PRIORITY)){
                    return 0;
                }
                return 1;
            }
        });

        return null;
    }

    private List<Get4SShopList.FSList> SortListByDis(List<Get4SShopList.FSList> list){

        Collections.sort(list, new Comparator<Get4SShopList.FSList>(){

            /*
             * 返回负数表示：o1 小于o2，
             * 返回0 表示：o1和o2相等，
             * 返回正数表示：o1大于o2。
             */
            public int compare(Get4SShopList.FSList o1, Get4SShopList.FSList o2) {

                //按照学生的年龄进行升序排列
                if(o1.DISTANCE > o2.DISTANCE){
                    return 1;
                }
                if(o1.DISTANCE == o2.DISTANCE){
                    return 0;
                }
                return -1;
            }
        });

        return null;
    }

    private void getDistance(){

        for(int i=0;i<mList.size();i++){

            mList.get(i).DISTANCE = Tools.LantitudeLongitudeDist(Double.parseDouble(JTApplication.lon),Double.parseDouble(JTApplication.lat),Double.parseDouble(mList.get(i).LON),Double.parseDouble(mList.get(i).LAT));

        }

    }
}
